// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: "test4-ht5oe"
})

const db=cloud.database();
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  try {

    if(event.tip=="user"){
      return await cloud.database().collection('MedicineData').where({
        MedicID:event.MID
      }).update({
        data:{
          MedicSurplus:_.inc(0-event.num),
          MedicSales:_.inc(event.num)
        }
        })
    }else{
    return await cloud.database().collection('MedicineData').where({
      MedicID:event.MID
    }).update({
      data:{
        MedicName:event.MName,
        MedicImage:event.MImage,
        MedicBriefIntro:event.MBriefIntro,
        MedicPrice:event.MPrice,
        MedicSurplus:Number(event.MSurplus),
        MedicDetailIntro:event.MDetailIntro,
        MedicSales:Number(event.MSales)
      }
      })
    }
  } catch (e) {
    console.error("ERROR",e)
  }
}